
import 'package:event_bus/event_bus.dart';
import 'package:flutter/material.dart';
import 'package:provide/provide.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import '../../provide/detailsDats.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import '../../eventbus/eventhbus.dart';

class DetailsTop extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    
    return Provide<DetailsInfoProvide>(
      builder: (context,child,val){
        var goodsInfo = Provide.value<DetailsInfoProvide>(context).goodsinfo.data;
        var url = goodsInfo.image;
        var name = goodsInfo.title;
        var price1 = goodsInfo.price;
        var price2 = goodsInfo.priceText;
        var goodid = goodsInfo.id;
        print("goodid ====================> ${goodid}");
        aaa(url,name,price1,price2,goodid);
        // print(url);
        // List<Map> goodsInfo.goodAdv.forEach((item) => print(item.advImg));
        List list = goodsInfo.productAdv;
        print(list.length);
        if(goodsInfo != null){
          return Container(
            color: Colors.white,
            child: Column(
              children: <Widget>[
                // _goodsImgs(goodsInfo.goodAdv[0].advImg),
                SwiperDiy(swiperData: list),
                _goodprice(goodsInfo.price,goodsInfo.priceText),
                _goodstype(goodsInfo.type),
                _goodsname(goodsInfo.title),
                // _goods_model(goodsInfo.goodsModel),
              ],
            ),
          );
        }else{
          return Text('正在加载中');
        }
      },
    );
  }


  //商品图片
  Widget _goodsImgs(url){
    return Image.network(
      url,
      width: ScreenUtil().setWidth(740),
    );
  }

  //产品类型
  Widget _goodstype(type){
    String typename = (type==1?'直营产品':type==2?'库存商品':'经销商品');
    return Container(
      width: ScreenUtil().setWidth(120),
      height: ScreenUtil().setHeight(40),
      margin: EdgeInsets.only(right: 320,bottom: 2.5,top: 2.5),
      decoration: BoxDecoration(
          color: Color.fromRGBO(0, 229, 238, 1),
          borderRadius: BorderRadius.circular(10)
      ),
      child: Text(
        typename,
        textAlign: TextAlign.center,
      ),
    );
  }


  //商品价格
  Widget _goodprice(price1,price2){
    return Container(
      width: ScreenUtil().setWidth(750),
      padding: EdgeInsets.only(top: 10.0,left: 15.0),
      color: Colors.grey,
      child: Row(
        children: <Widget>[
          Text(
            '会员价:￥${price1}',
            style: TextStyle(
              color: Colors.red,
              fontWeight: FontWeight.w800,
              fontSize: ScreenUtil().setSp(40)
            ),
          ),
          Text("(含税)",
          style: TextStyle(
            color: Colors.red,
            fontWeight: FontWeight.w800,
            fontSize: ScreenUtil().setSp(40)
          ),),
          Text("原价:￥${price2}", 
          style: TextStyle(
            color: Colors.black26,
          ),),
        ],
      ),
    );
  }

  //商品名称
  Widget _goodsname(name){
    return Container(
      width: ScreenUtil().setWidth(740),
      padding: EdgeInsets.only(left: 15.0),
      child: Text(
        name,
        style: TextStyle(
          fontSize: ScreenUtil().setSp(30.0)
        ),
      ),
    );
  }


  //商品型号
  // Widget _goods_model(model){
  //   return Container(
  //     // width: ScreenUtil().setWidth(730),
  //     padding: EdgeInsets.only(left: 15.0),
  //     margin: EdgeInsets.only(top: 8.0),
  //     color: Colors.red,
  //     child: Text(
  //       '型号:${model}',
  //       style:TextStyle(
  //         color:Colors.black
  //       ),
  //       textAlign: TextAlign.center,
  //     ),
  //   );
  // }

  aaa(url,name,price1,price2,goodid){
    // print(url);
    // print(name);
    // print(price1);
    // print(price2);
          eventBus.fire(
          Details(url,name,price1,price2,goodid)
        );
        }
}

class SwiperDiy extends StatelessWidget {
  final List swiperData;
  SwiperDiy({this.swiperData});
  @override
  Widget build(BuildContext context) {
    return Container(
      height: ScreenUtil().setHeight(652),
      width: ScreenUtil().setWidth(750),
      child: Swiper(
        itemBuilder: (BuildContext context, int index) {
          return Image.network(swiperData[index].image);
        },
        itemCount: swiperData.length,
        pagination: SwiperPagination(),
        autoplay: true,
      ),
    );
  }
}